package com.market.cash.ui.fragment

import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import com.market.cash.R
import com.market.cash.databinding.CashPinStatusFailedBinding
import com.market.cash.pro.ProFragment
import com.market.cash.ui.activity.KefuActivity
import com.market.cash.ui.activity.PinStatusActivity
import com.market.cash.utils.event.CashRootEventHelper.addCashRootRequestEventAction
import com.market.cash.utils.event.CashRootEventType
import com.market.cash.utils.helper.CashRootCustomConfigsHelper
import org.json.JSONObject

/**
 * CashRoot
 * @Author wwdeng
 * @CreateTime: 2024/10/25 17:04
 * @Annotation：支付失败
 */
class PinFailedFragment(): ProFragment(){
    lateinit var binding: CashPinStatusFailedBinding
    private var orderData: JSONObject? = null
    private var act: PinStatusActivity? = null

    constructor(act: PinStatusActivity, data: JSONObject): this(){
        this.act = act
        orderData = data
    }

    override fun getCashRootRootView(inflater: LayoutInflater): View {
        binding = CashPinStatusFailedBinding.inflate(inflater)
        return binding.root
    }

    override fun initCashRootView() {
        rootViewBinding.rootLayout.setBackgroundColor(Color.parseColor("#F8F8F8"))
        setCashRootTitleBarVisibility(View.VISIBLE)
        setCashRootBackBtnRes(R.mipmap.cash_back_black)
        setCashRootTextTitle(getString(R.string.loan_payment_status_title))
        setCashRootRightBtnRes(R.mipmap.cash_kefu_black)
        setCashRootTitleBarColor(Color.parseColor("#FFFFFF"))
    }

    override fun initCashRootListener() {
        //客服
        rootViewBinding.imgKefu.setOnClickListener {
            if (act != null){
                KefuActivity.start(requireContext(),"PIN PaymentFailed Page",act!!.loadPName(),act!!.loadPToken())
            }
            addCashRootRequestEventAction(CashRootEventType.CLICK.CASHROOT_CLICK_CUSTOMER, "PIN PaymentFailed Page")
        }
        binding.imgCopy.setOnClickListener {
            if (orderData != null){
                onCashRootCopyAction(orderData!!.optString("transactionId"))
            }
        }

        binding.btnPaymentAgain.setOnClickListener {
            addCashRootRequestEventAction(CashRootEventType.CLICK.CASHROOT_CLICK_LOAN_PAYMENT_FAILED_PAYMENT_AGAIN,"TransactionId：${orderData!!.optString("transactionId")}")
            act!!.presenter!!.loadCashRootOrderPayLinkAction()
        }
    }

    override fun initCashRootData() {
        refreshData(orderData!!)
        //牌照信息
        if (!CashRootCustomConfigsHelper.isCashRootShowLicense()){
            binding.licenseLayout.removeAllViews()
            binding.licenseLayout.visibility = View.GONE
        } else {
            binding.licenseLayout.removeAllViews()
            binding.licenseLayout.addView(initCashRootLicenseView())
            binding.licenseLayout.visibility = View.VISIBLE
        }
    }

    /**
     * 刷新数据
     */
    fun refreshData(data: JSONObject){
        orderData = data
        binding.textAmount.text = data.optString("realRepayAmount","")
        binding.textProduct.text = data.optString("product","")
        binding.textThirdOrderId.text = data.optString("transactionId","")
        binding.textFailedTime.text = data.optString("failedTime","")

        val failReason = data.optString("failReason","")
        if (failReason.isNotEmpty()){
            binding.textFailReason.visibility = View.VISIBLE
            binding.textFailReason.text = String.format(getString(R.string.loan_payment_status_failed_tips),failReason)
        } else {
            binding.textFailReason.visibility = View.GONE
        }
    }

    /**
     * 复制
     */
    private fun onCashRootCopyAction(orderId: String){
        addCashRootRequestEventAction(CashRootEventType.CLICK.CASHROOT_CLICK_LOAN_PAYMENT_FAILED_COPY, "TransactionId：$orderId")
        // 获取剪贴板管理器
        val clipboardManager = act!!.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
        // 创建一个 ClipData 对象，将要复制的文本放入其中
        val clipData = ClipData.newPlainText("label", orderId)
        // 将 ClipData 对象放入剪贴板
        clipboardManager.setPrimaryClip(clipData)
        showCashRootToast(getString(R.string.kefu_copy_success))
    }

    override fun onStart() {
        super.onStart()
        if (act != null){
            addCashRootRequestEventAction(CashRootEventType.PAGE.CASHROOT_PAGE_LOAN_PIN_REPAYMENT_FAILED_START,act!!.loadPName())
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        if (act != null){
            addCashRootRequestEventAction(CashRootEventType.PAGE.CASHROOT_PAGE_LOAN_PIN_REPAYMENT_FAILED_END,act!!.loadPName())
        }
    }
}